package com.demo.http.okhttp;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import com.demo.http.HttpCallback;

/**
 * Created by se1 on 16-11-16.
 */
public class HttpClient {

    private OkHttpClient client = null;

    private HttpClient() {
        client = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .build();
    }

    private static class SingletonHolder {
        private final static HttpClient INSTANCE = new HttpClient();
    }

    public static HttpClient getInstance() {
        return HttpClient.SingletonHolder.INSTANCE;
    }

    public void get(String host, Map<String, String> params, final HttpCallback callback) {
        StringBuilder stringBuilder = new StringBuilder(host);
        if (null != params && !params.isEmpty()) {
            stringBuilder.append("?");
            Iterator it = params.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry element = (Map.Entry) it.next();
                stringBuilder.append(element.getKey());
                stringBuilder.append("=");
                stringBuilder.append(element.getValue());
                stringBuilder.append("&");
            }
        }
        String url = stringBuilder.toString();

        Request request = new Request.Builder()
                .url(url)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                if (null != callback) {
                    callback.onFailure(0, e);
                }
            }

            @Override
            public void onResponse(Call call, Response response) {
                try {
                    int responseCode = response.code();
                    if (200 == responseCode) {
                        if (null != callback) {
                            callback.onResponse(response.body().bytes());
                        }
                    } else {
                        callback.onFailure(responseCode, null);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    if (null != callback) {
                        callback.onFailure(0, e);
                    }
                }
            }
        });
    }

    public void post(String host, Map<String, String> params, final HttpCallback callback) {
        FormBody.Builder builder = new FormBody.Builder();
        Iterator it = params.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry element = (Map.Entry) it.next();
            builder.add((String) element.getKey(), (String) element.getValue());
        }
        RequestBody body = builder.build();

        Request request = new Request.Builder()
                .url(host)
                .post(body)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                if (null != callback) {
                    callback.onFailure(0, e);
                }
            }

            @Override
            public void onResponse(Call call, Response response) {
                try {
                    int responseCode = response.code();
                    if (200 == responseCode) {
                        if (null != callback) {
                            callback.onResponse(response.body().bytes());
                        }
                    } else {
                        callback.onFailure(responseCode, null);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    if (null != callback) {
                        callback.onFailure(0, e);
                    }
                }
            }
        });
    }
}
